package com.zwan.oam_rtc.utils;

import com.zwan.oam_rtc.utils.wintcp.MD5;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @version 0.0.1
 * @authors svip955
 * @date 2018/12/6 9:43
 */
public class Zwgwparam {

    /***
     * jdk 1.8以下
     * zwgw加密算法
     * @param maps
     * @param companykey
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map paramMap1(Map<String, String> maps , String companykey) {
        Map<String, String> sortMap = new TreeMap<String, String>(new MapComparator());
        sortMap.putAll(maps);
        String param = "";
        for (String key : sortMap.keySet()) {
            param += (key + "=" + sortMap.get(key) + "&");
        }
        param = (param.length() > 1) ? param.substring(0, (param.length() - 1)) : "";
        String digest = MD5.doit(MD5.doit(param).toLowerCase() + companykey).toLowerCase();
        maps.put("digest", digest);
        return maps;
    }

    public static String Getdigest1(Map<String, String> maps ,String companykey) {
        Map<String, String> sortMap = new TreeMap<String, String>(new MapComparator());
        sortMap.putAll(maps);
        String param = "";
        for (String key : sortMap.keySet()) {
            param += (key + "=" + sortMap.get(key) + "&");
        }
        param = (param.length() > 1) ? param.substring(0, (param.length() - 1)) : "";
        return  MD5.doit(MD5.doit(param).toLowerCase() + companykey).toLowerCase();
    }

    /***
     * jdk 1.8
     * zwgw加密算法
     * @param maps
     * @param companykey
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map paramMap(Map<String, String> maps ,String companykey) {
        Map<String, String> finalMap = new TreeMap<>();
        maps.entrySet().stream()
                .sorted(Map.Entry.<String, String>comparingByKey()
                        .reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
        String param = "";
        for (String key : finalMap.keySet()) {
            param += (key + "=" + finalMap.get(key) + "&");
        }
        param = (param.length() > 1) ? param.substring(0, (param.length() - 1)) : "";
        String digest = MD5.doit(MD5.doit(param).toLowerCase() + companykey).toLowerCase();
        maps.put("digest", digest);
        //System.out.println("time:"+new Date()+" |body:"+maps.toString());
        return maps;
    }

    public static String Getdigest(Map<String, String> maps ,String companykey) {
        Map<String, String> finalMap = new TreeMap<>();
        maps.entrySet().stream()
                .sorted(Map.Entry.<String, String>comparingByKey()
                        .reversed()).forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue()));
        String param = "";
        for (String key : finalMap.keySet()) {
            param += (key + "=" + finalMap.get(key) + "&");
        }
        param = (param.length() > 1) ? param.substring(0, (param.length() - 1)) : "";
        return  MD5.doit(MD5.doit(param).toLowerCase() + companykey).toLowerCase();
    }


}

//比较器类
class MapComparator implements Comparator<String> {

    @Override
    public int compare(String me1, String me2) {
        return me1.compareTo(me2);
    }
}